<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>C++编程模板 | whoway</title>
    <meta name="description" content="Personal Blog Website">
    <link rel="icon" href="/images/photo.jpg">
  <link rel="manifest" href="/images/photo.jpg">
  <link rel="apple-touch-icon" href="/images/photo.jpg">
  <meta http-quiv="pragma" cotent="no-cache">
  <meta http-quiv="pragma" cotent="no-cache,must-revalidate">
  <meta http-quiv="expires" cotent="0">
    
    <link rel="preload" href="/assets/css/0.styles.0dbae9ec.css" as="style"><link rel="preload" href="/assets/js/app.c70e21ad.js" as="script"><link rel="preload" href="/assets/js/57.a9caec0d.js" as="script"><link rel="prefetch" href="/assets/js/10.15222a53.js"><link rel="prefetch" href="/assets/js/100.7e0e5a86.js"><link rel="prefetch" href="/assets/js/101.efd59f25.js"><link rel="prefetch" href="/assets/js/102.dfbdc06c.js"><link rel="prefetch" href="/assets/js/103.d3ab2109.js"><link rel="prefetch" href="/assets/js/104.117957ef.js"><link rel="prefetch" href="/assets/js/105.046e8ff3.js"><link rel="prefetch" href="/assets/js/106.aebdc17d.js"><link rel="prefetch" href="/assets/js/107.248733c2.js"><link rel="prefetch" href="/assets/js/108.a2fecadc.js"><link rel="prefetch" href="/assets/js/109.35196857.js"><link rel="prefetch" href="/assets/js/11.770642f2.js"><link rel="prefetch" href="/assets/js/110.cf3d973c.js"><link rel="prefetch" href="/assets/js/111.f985889a.js"><link rel="prefetch" href="/assets/js/112.ad614f41.js"><link rel="prefetch" href="/assets/js/113.f666653c.js"><link rel="prefetch" href="/assets/js/114.c6c3f384.js"><link rel="prefetch" href="/assets/js/115.e51d3c2f.js"><link rel="prefetch" href="/assets/js/116.4f4b39f5.js"><link rel="prefetch" href="/assets/js/117.99352e11.js"><link rel="prefetch" href="/assets/js/118.c6ae6572.js"><link rel="prefetch" href="/assets/js/119.4ccbe778.js"><link rel="prefetch" href="/assets/js/12.042a92ff.js"><link rel="prefetch" href="/assets/js/120.edda1c4f.js"><link rel="prefetch" href="/assets/js/121.30a638ed.js"><link rel="prefetch" href="/assets/js/122.6efcefb1.js"><link rel="prefetch" href="/assets/js/123.91e6665b.js"><link rel="prefetch" href="/assets/js/124.f27e3d7e.js"><link rel="prefetch" href="/assets/js/125.c75712d5.js"><link rel="prefetch" href="/assets/js/126.ed756cce.js"><link rel="prefetch" href="/assets/js/127.2f06c74c.js"><link rel="prefetch" href="/assets/js/128.d5f6f30e.js"><link rel="prefetch" href="/assets/js/129.508b7eed.js"><link rel="prefetch" href="/assets/js/13.b5280c37.js"><link rel="prefetch" href="/assets/js/130.dc05f9aa.js"><link rel="prefetch" href="/assets/js/131.e0ba69b1.js"><link rel="prefetch" href="/assets/js/132.d79bcaa4.js"><link rel="prefetch" href="/assets/js/133.34acc01a.js"><link rel="prefetch" href="/assets/js/134.dabf64d5.js"><link rel="prefetch" href="/assets/js/135.ad90c915.js"><link rel="prefetch" href="/assets/js/136.dbb0074f.js"><link rel="prefetch" href="/assets/js/137.284ad365.js"><link rel="prefetch" href="/assets/js/138.a4b6856f.js"><link rel="prefetch" href="/assets/js/139.c9c1e20f.js"><link rel="prefetch" href="/assets/js/14.df02ba38.js"><link rel="prefetch" href="/assets/js/140.8b0a9269.js"><link rel="prefetch" href="/assets/js/141.9c7759c5.js"><link rel="prefetch" href="/assets/js/142.a4201a82.js"><link rel="prefetch" href="/assets/js/143.d7da6e8c.js"><link rel="prefetch" href="/assets/js/144.5e48e65d.js"><link rel="prefetch" href="/assets/js/145.a0e2633c.js"><link rel="prefetch" href="/assets/js/146.3c775f9b.js"><link rel="prefetch" href="/assets/js/147.22add89a.js"><link rel="prefetch" href="/assets/js/148.cfde1009.js"><link rel="prefetch" href="/assets/js/149.ffc835b5.js"><link rel="prefetch" href="/assets/js/15.fbdfc4ee.js"><link rel="prefetch" href="/assets/js/150.406c4b20.js"><link rel="prefetch" href="/assets/js/151.b2040eea.js"><link rel="prefetch" href="/assets/js/152.7bc65661.js"><link rel="prefetch" href="/assets/js/153.1d7c65e3.js"><link rel="prefetch" href="/assets/js/154.1309de49.js"><link rel="prefetch" href="/assets/js/155.81d3ee1f.js"><link rel="prefetch" href="/assets/js/156.154a4ef2.js"><link rel="prefetch" href="/assets/js/16.e5eb6147.js"><link rel="prefetch" href="/assets/js/17.57853c4a.js"><link rel="prefetch" href="/assets/js/18.cb9d7518.js"><link rel="prefetch" href="/assets/js/19.f354dc47.js"><link rel="prefetch" href="/assets/js/2.570d8a23.js"><link rel="prefetch" href="/assets/js/20.b5af7fad.js"><link rel="prefetch" href="/assets/js/21.0b1928fe.js"><link rel="prefetch" href="/assets/js/22.f78666de.js"><link rel="prefetch" href="/assets/js/23.29c3f366.js"><link rel="prefetch" href="/assets/js/24.6f596516.js"><link rel="prefetch" href="/assets/js/25.14067b60.js"><link rel="prefetch" href="/assets/js/26.74ba4989.js"><link rel="prefetch" href="/assets/js/27.13d60edd.js"><link rel="prefetch" href="/assets/js/28.9523cb32.js"><link rel="prefetch" href="/assets/js/29.8ec842e9.js"><link rel="prefetch" href="/assets/js/3.3fb3d2e0.js"><link rel="prefetch" href="/assets/js/30.805597a8.js"><link rel="prefetch" href="/assets/js/31.831b195d.js"><link rel="prefetch" href="/assets/js/32.063c672d.js"><link rel="prefetch" href="/assets/js/33.6d93fac3.js"><link rel="prefetch" href="/assets/js/34.56e8263c.js"><link rel="prefetch" href="/assets/js/35.dbe688bb.js"><link rel="prefetch" href="/assets/js/36.dc5af2c1.js"><link rel="prefetch" href="/assets/js/37.0a7494f6.js"><link rel="prefetch" href="/assets/js/38.fe4fc171.js"><link rel="prefetch" href="/assets/js/39.f5ed5e92.js"><link rel="prefetch" href="/assets/js/4.2c405ec8.js"><link rel="prefetch" href="/assets/js/40.fe7e2714.js"><link rel="prefetch" href="/assets/js/41.30b0811d.js"><link rel="prefetch" href="/assets/js/42.76f52d62.js"><link rel="prefetch" href="/assets/js/43.e7bb0817.js"><link rel="prefetch" href="/assets/js/44.ead0e883.js"><link rel="prefetch" href="/assets/js/45.235df046.js"><link rel="prefetch" href="/assets/js/46.5f09e829.js"><link rel="prefetch" href="/assets/js/47.67116354.js"><link rel="prefetch" href="/assets/js/48.31f34543.js"><link rel="prefetch" href="/assets/js/49.10b5ebba.js"><link rel="prefetch" href="/assets/js/5.6f47322c.js"><link rel="prefetch" href="/assets/js/50.c0f0b7f1.js"><link rel="prefetch" href="/assets/js/51.5143f3bf.js"><link rel="prefetch" href="/assets/js/52.eeddfd48.js"><link rel="prefetch" href="/assets/js/53.eb790db5.js"><link rel="prefetch" href="/assets/js/54.8fe5421c.js"><link rel="prefetch" href="/assets/js/55.d8f9004b.js"><link rel="prefetch" href="/assets/js/56.62ac9b92.js"><link rel="prefetch" href="/assets/js/58.f93fc522.js"><link rel="prefetch" href="/assets/js/59.a81a03aa.js"><link rel="prefetch" href="/assets/js/6.8c2ea393.js"><link rel="prefetch" href="/assets/js/60.ab782775.js"><link rel="prefetch" href="/assets/js/61.6dd12daf.js"><link rel="prefetch" href="/assets/js/62.76f4b01f.js"><link rel="prefetch" href="/assets/js/63.6f8a4742.js"><link rel="prefetch" href="/assets/js/64.6f8bb1fa.js"><link rel="prefetch" href="/assets/js/65.4120a44b.js"><link rel="prefetch" href="/assets/js/66.360c2d2b.js"><link rel="prefetch" href="/assets/js/67.26f84d32.js"><link rel="prefetch" href="/assets/js/68.68f45e5e.js"><link rel="prefetch" href="/assets/js/69.e311eb56.js"><link rel="prefetch" href="/assets/js/7.6762b2d7.js"><link rel="prefetch" href="/assets/js/70.cea82674.js"><link rel="prefetch" href="/assets/js/71.783ddcf7.js"><link rel="prefetch" href="/assets/js/72.e5467385.js"><link rel="prefetch" href="/assets/js/73.b8fb681b.js"><link rel="prefetch" href="/assets/js/74.1bae37db.js"><link rel="prefetch" href="/assets/js/75.024387e5.js"><link rel="prefetch" href="/assets/js/76.a8e53010.js"><link rel="prefetch" href="/assets/js/77.8c55500a.js"><link rel="prefetch" href="/assets/js/78.7ce90bf5.js"><link rel="prefetch" href="/assets/js/79.ef71713f.js"><link rel="prefetch" href="/assets/js/8.788a6364.js"><link rel="prefetch" href="/assets/js/80.acad589d.js"><link rel="prefetch" href="/assets/js/81.02670d10.js"><link rel="prefetch" href="/assets/js/82.53b7b1ac.js"><link rel="prefetch" href="/assets/js/83.99eb8581.js"><link rel="prefetch" href="/assets/js/84.d1535ce3.js"><link rel="prefetch" href="/assets/js/85.fe2b7de9.js"><link rel="prefetch" href="/assets/js/86.41850272.js"><link rel="prefetch" href="/assets/js/87.1cdc6df9.js"><link rel="prefetch" href="/assets/js/88.01bf3461.js"><link rel="prefetch" href="/assets/js/89.17c69819.js"><link rel="prefetch" href="/assets/js/9.3813842d.js"><link rel="prefetch" href="/assets/js/90.f6ae7e35.js"><link rel="prefetch" href="/assets/js/91.507bc284.js"><link rel="prefetch" href="/assets/js/92.90551782.js"><link rel="prefetch" href="/assets/js/93.dc442d78.js"><link rel="prefetch" href="/assets/js/94.315f4e94.js"><link rel="prefetch" href="/assets/js/95.ccd6c6bf.js"><link rel="prefetch" href="/assets/js/96.0c6d89d0.js"><link rel="prefetch" href="/assets/js/97.1a9f10a9.js"><link rel="prefetch" href="/assets/js/98.43be3caa.js"><link rel="prefetch" href="/assets/js/99.54c8207b.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0dbae9ec.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">whoway</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link router-link-active">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link router-link-active">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>C++编程模板</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#✔️01-常量定义和变量名" class="sidebar-link">✔️01.常量定义和变量名</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#✔️02-格式化输出模板" class="sidebar-link">✔️02.格式化输出模板</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#✔️03-『四舍五入』模板" class="sidebar-link">✔️03.『四舍五入』模板</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_3、注意事项" class="sidebar-link">3、注意事项</a></li></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#acm课程" class="sidebar-link">ACM课程</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#浮点数，计算的精度" class="sidebar-link">浮点数，计算的精度</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#ac的代码" class="sidebar-link">AC的代码</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#一行区别的wa" class="sidebar-link">一行区别的WA</a></li></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#⭐️c-通用编码方式" class="sidebar-link">⭐️C++通用编码方式</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_1-1-判断某个整数奇-偶" class="sidebar-link">1.1.判断某个整数奇/偶</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_1-2-判断是否非0" class="sidebar-link">1.2.判断是否非0</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_1-3-二维数组遍历技巧『✔️技巧』" class="sidebar-link">1.3.二维数组遍历技巧『✔️技巧』</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_1-4-函数random-shuffle打乱『✔️c-』" class="sidebar-link">1.4.函数random_shuffle打乱『✔️C++』</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_1-5-给二维的vector初始化row和col『✔️c-』" class="sidebar-link">1.5.给二维的vector初始化row和col『✔️C++』</a></li></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#⭐️常用内容记忆" class="sidebar-link">⭐️常用内容记忆</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_2-1-常用字符函数『记忆』" class="sidebar-link">2.1.常用字符函数『记忆』</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_2-2-字符串比较『记忆法』" class="sidebar-link">2.2.字符串比较『记忆法』</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_2-3-记忆g-中的宏✔️" class="sidebar-link">2.3.记忆g++中的宏✔️</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_2-4-常见数据表示的范围" class="sidebar-link">2.4.常见数据表示的范围</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_2-4-long-long和double在icpc中" class="sidebar-link">2.4.long long和double在ICPC中</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#常见错误答案" class="sidebar-link">常见错误答案</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#解决方案" class="sidebar-link">解决方案</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_2-5-✔️位运算实现大小写转换" class="sidebar-link">2.5.✔️位运算实现大小写转换</a></li></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#⭐️输入输出提升" class="sidebar-link">⭐️输入输出提升</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_3-1-cin和scanf速度相关" class="sidebar-link">3.1.cin和scanf速度相关</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_3-2-getchar设计read函数快速读入（正负）" class="sidebar-link">3.2.getchar设计read函数快速读入（正负）</a></li></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#⭐️类型转换记忆" class="sidebar-link">⭐️类型转换记忆</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_4-1-函数to-string-将数字转换为string『⭐️c-11』" class="sidebar-link">4.1.函数to_string 将数字转换为string『⭐️C++11』</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_4-2-string→int-long-long-double等" class="sidebar-link">4.2.string→int/long long /double等</a></li></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#⭐️sscanf-sprintf的使用和记忆方法" class="sidebar-link">⭐️sscanf/sprintf的使用和记忆方法</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#✔️memset和fill" class="sidebar-link">✔️memset和fill</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#⭐️c-中string⇔c语言风格的字符数组⇔char" class="sidebar-link">⭐️C++中string⇔C语言风格的字符数组⇔char</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_5-1-string→字符数组" class="sidebar-link">5.1.string→字符数组</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_5-2-字符数组→string" class="sidebar-link">5.2.字符数组→string</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_5-3-转换string→char" class="sidebar-link">5.3.转换string→char</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_5-4-转换char→string" class="sidebar-link">5.4.转换char→string</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#_5-5-c语言字符数组和字符到string类" class="sidebar-link">5.5.C语言字符数组和字符到string类</a></li><li class="sidebar-sub-header"><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#✔️关于c-的string建议：" class="sidebar-link">✔️关于C++的string建议：</a></li></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#✔️int-char-string" class="sidebar-link">✔️int-&gt;char-&gt;string</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/01.DataStructureAndAlgorithm/Part02.C%2B%2B%E7%BC%96%E7%A8%8B%E6%A8%A1%E6%9D%BF.html#『逆向思维』编程技巧" class="sidebar-link">『逆向思维』编程技巧</a><ul class="sidebar-sub-headers"></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="c-编程模板"><a href="#c-编程模板" class="header-anchor">#</a> C++编程模板</h1> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>&lt;font style=&quot;background:yellow&quot;&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="✔️01-常量定义和变量名"><a href="#✔️01-常量定义和变量名" class="header-anchor">#</a> ✔️01.常量定义和变量名</h2> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">static</span> <span class="token keyword">const</span> <span class="token keyword">int</span> maxn<span class="token operator">=</span><span class="token number">1e5</span><span class="token operator">+</span><span class="token number">5</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="✔️02-格式化输出模板"><a href="#✔️02-格式化输出模板" class="header-anchor">#</a> ✔️02.格式化输出模板</h2> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">while</span><span class="token punctuation">(</span> loop<span class="token operator">--</span> <span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d%c&quot;</span><span class="token punctuation">,</span>solve<span class="token punctuation">[</span>loop<span class="token punctuation">]</span><span class="token punctuation">,</span> loop<span class="token operator">?</span><span class="token string">' '</span><span class="token operator">:</span> <span class="token string">'\n'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 
    <span class="token comment">//32是空格的ASCII，10是换行符号的ASCII</span>
    <span class="token comment">//等价于printf(&quot;%d%c&quot;,solve[loop], loop?: 32 : 10);</span>
    <span class="token comment">//ASCII炫技，很不妥了，可读性很尴尬</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>下面的有2个技巧</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token comment">//第1个地方，这个方式可以用于区分在本地和OJ</span>
<span class="token comment">//#define HACV	</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">10</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
        <span class="token comment">//第2个地方，和前面炫技有异曲同工之妙</span>
		<span class="token function">printf</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>i<span class="token operator">!=</span><span class="token number">9</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">&quot;num=%d\n&quot;</span> <span class="token operator">:</span> <span class="token string">&quot;Last num=%d\n&quot;</span> <span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">ifdef</span> <span class="token expression">HACV</span></span>
	<span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;pause&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">endif</span></span>

	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h2 id="✔️03-『四舍五入』模板"><a href="#✔️03-『四舍五入』模板" class="header-anchor">#</a> ✔️03.『四舍五入』模板</h2> <p><strong>1、printf的格式化输出</strong></p> <ul><li><p><code>%3d</code>表示输出3位数，不满3位的高位补『空格』</p></li> <li><p><code>%03d</code>表示输出3位整数，不满3位的高位补『0』</p></li></ul> <p><strong>2、四舍五入『模板』</strong></p> <ul><li><code>+0.5</code>是精髓</li> <li>但是，一直，不晓得是哪节课，讲解了sqrt在之前还是啥的，要和0.5啥的
？？？哪个技巧没做到笔记（后面找到了笔记，eps）</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">double</span> num<span class="token operator">=</span><span class="token number">3.5</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//直接截取</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>num<span class="token operator">+</span><span class="token number">0.5</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//四舍五入</span>

<span class="token comment">//num=(num*pow(10,2)+0.5)/pow(10,2);  </span>
<span class="token comment">//注意，pow(10,需要保留的位数)</span>
<span class="token comment">////如果采用『四舍五入』</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;第%d名选手的最好成绩为:%.2lf\n&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">-</span>loop<span class="token punctuation">)</span><span class="token punctuation">,</span> num <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;math.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdlib.h&gt;</span></span>


<span class="token keyword">struct</span> <span class="token class-name">node</span>
<span class="token punctuation">{</span>
	<span class="token keyword">double</span> val<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> solve<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>


<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>



	<span class="token comment">//第（3-loop）个选手的成绩</span>
	<span class="token keyword">int</span> loop<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span> loop<span class="token operator">--</span> <span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;请输入第%d名选手的三次成绩\n&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">-</span>loop<span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token comment">//输入第（tag+1）个成绩</span>
		<span class="token keyword">int</span> tag<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span> tag<span class="token operator">&lt;</span><span class="token number">3</span> <span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			<span class="token comment">//如果输入错误则要求强制重新输入</span>
			<span class="token keyword">while</span><span class="token punctuation">(</span> <span class="token number">1</span> <span class="token punctuation">)</span>
			<span class="token punctuation">{</span>	
				<span class="token keyword">double</span> num<span class="token punctuation">;</span>
				<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%lf&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token punctuation">(</span>num<span class="token operator">-</span><span class="token number">0.0</span><span class="token punctuation">)</span><span class="token operator">&gt;</span><span class="token number">0.0001</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span>num<span class="token operator">-</span><span class="token number">10.0</span><span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token number">0.0001</span> <span class="token punctuation">)</span>
				<span class="token punctuation">{</span>
					solve<span class="token punctuation">[</span>loop<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">[</span>tag<span class="token punctuation">]</span><span class="token operator">=</span>num<span class="token punctuation">;</span>
					<span class="token keyword">break</span><span class="token punctuation">;</span><span class="token comment">//关键之处：用while(1)和break的组合来弄出那样的效果</span>
				<span class="token punctuation">}</span>
				<span class="token keyword">else</span>
				<span class="token punctuation">{</span>
					<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;输入错误,请重新输入！\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">}</span>
			<span class="token punctuation">}</span><span class="token comment">//#end while( 1 )</span>

			<span class="token operator">++</span>tag<span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token comment">//#end while( tag&lt;3 )</span>

			
	<span class="token punctuation">}</span><span class="token comment">//#end while( loop-- )</span>


	loop<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span> loop<span class="token operator">--</span> <span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">double</span> num<span class="token operator">=</span>solve<span class="token punctuation">[</span>loop<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
		<span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">3</span><span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span> num<span class="token operator">&lt;</span>solve<span class="token punctuation">[</span>loop<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token punctuation">)</span>
			<span class="token punctuation">{</span>
				num<span class="token operator">=</span>solve<span class="token punctuation">[</span>loop<span class="token punctuation">]</span><span class="token punctuation">.</span>val<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
		<span class="token punctuation">}</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;第%d名选手的最好成绩为:%.2lf\n&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token number">3</span><span class="token operator">-</span>loop<span class="token punctuation">)</span><span class="token punctuation">,</span> num <span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token punctuation">}</span>


	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br></div></div><h3 id="_3、注意事项"><a href="#_3、注意事项" class="header-anchor">#</a> 3、注意事项</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token operator">%</span>d   是直接将<span class="token keyword">double</span>第<span class="token number">1</span>位小数截取
    
<span class="token operator">%</span><span class="token number">.1f</span> 是指将第二位小数直接去掉还是要四舍五入？
    
    答：<span class="token operator">%</span><span class="token number">.1f</span> <span class="token punctuation">(</span>注意是<span class="token number">1</span><span class="token punctuation">)</span>对第二位小数，四舍五入！！
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><div class="language-txt line-numbers-mode"><pre class="language-text"><code>%.2lf竟然是四舍五入的！

以此留念。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>C语言printf函数<span class="token operator">%</span><span class="token number">.2f</span>输出为什么四舍五入实现机制不同？
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><ul><li>知乎上<a href="https://www.zhihu.com/question/364159510" target="_blank" rel="noopener noreferrer">回答<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h2 id="acm课程"><a href="#acm课程" class="header-anchor">#</a> ACM课程</h2> <p>讲解了：</p> <blockquote><ul><li>现在的ACM不是很卡内存了
所以，上课没讲空间复杂度（这逻辑绝了）
现在不讲空间复杂度了，比如长春赛区，开了二维线段树，很大，接近4G也AC了
所以现在，一般你的电脑能开多大内存，一般也能给你弄
<strong>现在比赛一般不卡内存了</strong>。。。。。。</li></ul></blockquote> <blockquote><ul><li>还是有<strong>卡内存</strong>的题目，比如<strong>幻方矩阵</strong>。</li></ul></blockquote> <p><strong>比赛也很少考常数（常数级别算法？）的，也就是1-2倍的区别</strong>
比赛一般放8倍</p> <p>暴力算法是我们进步的阶梯</p> <p>每次自己写快速排序，，不。每早，打快排</p> <p>(也就是公众号中所说的，我们要做的事情是熟练)</p> <p>string公式要记忆，组合数，估计阶乘，估计，可以使得题目简单</p> <ul><li>2020-09-12 eps经验</li></ul> <p>负值，不能开根号啦</p> <h3 id="浮点数，计算的精度"><a href="#浮点数，计算的精度" class="header-anchor">#</a> 浮点数，计算的精度</h3> <p>是的，开根号有误差
第二，在X取整之前需要加0.05，为什么不加？</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token number">6.99</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>//上面强制类型转换后是6而不是7</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>1, 10, 100, 1000...
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB
Total submit users: 915, Accepted users: 691
Problem 10238 : No special judgement
Problem description
Let's consider an infinite sequence of digits constructed of ascending powers of 10 written one after another. Here is the beginning of the sequence: 110100100010000... You are to find out what digit is located at the definite position of the sequence.

Input
There is the only positive integer number N in the first line, N &lt; 65536. The i-th of N left lines contains the positive integer Ki - the number of position in the sequence. It's given that Ki &lt; 231..

Output
You are to output N digits 0 or 1 separated with a space. More precisely, the i-th digit of output is to be equal to the Ki-th digit of described above sequence.

Sample Input
4
3
14
7
6
Sample Output
0 0 1 0
Problem Source
USU Open. October'2002 Junior
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><h3 id="ac的代码"><a href="#ac的代码" class="header-anchor">#</a> AC的代码</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cstdio&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cmath&gt;</span></span>

<span class="token keyword">const</span> <span class="token keyword">double</span> eps<span class="token operator">=</span><span class="token number">0.05</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">double</span> x<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token number">8.0</span><span class="token operator">*</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token comment">//用这种，0.05的来在强制转换（int)前是为了防止</span>
	<span class="token comment">//本来经过很多次计算，应该获得是（int)7.00，最终是7</span>
	<span class="token comment">//但是可能出现(int)6.999就变成了6这样的情况 </span>
	<span class="token comment">//但是有一说一，有这比较方式，还不如，直接fabs(x-x)&lt;=eps </span>
	<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token operator">==</span>x<span class="token operator">-</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>x<span class="token operator">+</span>eps<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">else</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
<span class="token punctuation">}</span> 
 
 
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">int</span> n<span class="token punctuation">;</span>
	<span class="token keyword">int</span> temp<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">~</span><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>n<span class="token operator">--</span><span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>temp<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token comment">//可读性极差，。。</span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d%c&quot;</span><span class="token punctuation">,</span><span class="token function">test</span><span class="token punctuation">(</span>temp<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">,</span>n<span class="token operator">?</span><span class="token number">32</span><span class="token operator">:</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><h3 id="一行区别的wa"><a href="#一行区别的wa" class="header-anchor">#</a> 一行区别的WA</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cstdio&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cmath&gt;</span></span>

<span class="token keyword">const</span> <span class="token keyword">double</span> eps<span class="token operator">=</span><span class="token number">0.05</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">double</span> x<span class="token operator">=</span><span class="token function">sqrt</span><span class="token punctuation">(</span><span class="token number">8.0</span><span class="token operator">*</span>n<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">//用这种，0.05的来在强制转换（int)前是为了防止</span>
    <span class="token comment">//本来经过很多次计算，应该获得是（int)7.00，最终是7</span>
    <span class="token comment">//但是可能出现(int)6.999就变成了6这样的情况 </span>
    <span class="token comment">//但是有一说一，有这比较方式，还不如，直接fabs(x-x)&lt;=eps </span>
    <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token operator">==</span>x<span class="token operator">-</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">(</span>x<span class="token operator">+</span>eps<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">else</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span> 
 
 
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> n<span class="token punctuation">;</span>
    <span class="token keyword">int</span> temp<span class="token punctuation">;</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">~</span><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">)</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">while</span><span class="token punctuation">(</span>n<span class="token operator">--</span><span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>temp<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//32是空格的ASCII，10是换行符号的ASCII</span>
            <span class="token comment">//这样写代码很炫，但是，可读性极差，。。。炫技 </span>
<span class="token comment">//            printf(&quot;%d%c&quot;,test(temp-1),n?32:10);</span>
            <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span><span class="token function">test</span><span class="token punctuation">(</span>temp<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br></div></div><h2 id="⭐️c-通用编码方式"><a href="#⭐️c-通用编码方式" class="header-anchor">#</a> ⭐️C++通用编码方式</h2> <h3 id="_1-1-判断某个整数奇-偶"><a href="#_1-1-判断某个整数奇-偶" class="header-anchor">#</a> 1.1.判断某个整数奇/偶</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> n<span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span> n<span class="token operator">&amp;</span><span class="token number">1</span> <span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token comment">//奇数</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span>
<span class="token punctuation">{</span>
	<span class="token comment">//偶数</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_1-2-判断是否非0"><a href="#_1-2-判断是否非0" class="header-anchor">#</a> 1.2.判断是否非0</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> n<span class="token punctuation">;</span>
<span class="token keyword">if</span><span class="token punctuation">(</span> n <span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token comment">//n=1，还有n=-1啥的</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="_1-3-二维数组遍历技巧『✔️技巧』"><a href="#_1-3-二维数组遍历技巧『✔️技巧』" class="header-anchor">#</a> 1.3.二维数组遍历技巧『✔️技巧』</h3> <ul><li>使用余数和模来确定二维数组的Row和Col
<ul><li>行=pos/<strong>col『col表示列』</strong></li> <li>列=pos%<strong>col『col表示列』</strong></li> <li>记忆方法：<font style="background:yellow;">裂(列)开了</font></li></ul></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>vector<span class="token operator">&lt;</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;&gt;</span> <span class="token function">ans</span><span class="token punctuation">(</span>row<span class="token punctuation">,</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token punctuation">(</span>col<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">//row行col列的数组</span>

<span class="token comment">//注意：pos是从0到『m*n』！</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> pos <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> pos <span class="token operator">&lt;</span> m <span class="token operator">*</span> n<span class="token punctuation">;</span> <span class="token operator">++</span>pos<span class="token punctuation">)</span> 
<span class="token punctuation">{</span>
    ans<span class="token punctuation">[</span> pos<span class="token operator">/</span>col <span class="token punctuation">]</span><span class="token punctuation">[</span> pos<span class="token operator">%</span>col <span class="token punctuation">]</span> <span class="token operator">=</span> nums<span class="token punctuation">[</span> pos<span class="token operator">/</span>n <span class="token punctuation">]</span><span class="token punctuation">[</span> pos<span class="token operator">%</span>n <span class="token punctuation">]</span><span class="token punctuation">;</span>	<span class="token comment">//获得ans数组的行和列</span>
<span class="token punctuation">}</span>

<span class="token keyword">return</span> ans<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><ul><li>技巧参考自：<a href="https://leetcode-cn.com/problems/reshape-the-matrix/solution/zhong-su-ju-zhen-by-leetcode-solution-gt0g/" target="_blank" rel="noopener noreferrer">LeetCode题解<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h3 id="_1-4-函数random-shuffle打乱『✔️c-』"><a href="#_1-4-函数random-shuffle打乱『✔️c-』" class="header-anchor">#</a> 1.4.函数<code>random_shuffle</code>打乱『✔️C++』</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//1、播种随机数种子，如果不播种，也行，只是每次运行随机都是一样的。 </span>
<span class="token function">srand</span><span class="token punctuation">(</span><span class="token function">time</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">random_shuffle</span><span class="token punctuation">(</span> arr<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">,</span> arr<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//arr是vector</span>
<span class="token function">reverse</span><span class="token punctuation">(</span> arr<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> arr<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//arr是vector</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_1-5-给二维的vector初始化row和col『✔️c-』"><a href="#_1-5-给二维的vector初始化row和col『✔️c-』" class="header-anchor">#</a> 1.5.给二维的vector初始化row和col『✔️C++』</h3> <ul><li>记忆技巧：</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//初始化solve，行为3，列为4</span>
vector<span class="token operator">&lt;</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token operator">&gt;</span>  <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">3</span> <span class="token punctuation">,</span> vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>  
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>解释：临时变量<code>vector&lt;int&gt;(4)</code>表示：弄一个含有4个元素的<code>vector&lt;int&gt;</code></li></ul> <h2 id="⭐️常用内容记忆"><a href="#⭐️常用内容记忆" class="header-anchor">#</a> ⭐️常用内容记忆</h2> <h3 id="_2-1-常用字符函数『记忆』"><a href="#_2-1-常用字符函数『记忆』" class="header-anchor">#</a> 2.1.常用字符函数『记忆』</h3> <p><font style="background:yellow;">注意：is表示，判别是否</font></p> <ul><li>常用的『判别7子』</li> <li>分类函数,所在函数库为<code>ctype.h</code></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//如果是'0'-'9'返回对应的ASCII码，否则返回0『表示不是』</span>
<span class="token keyword">int</span> <span class="token function">isdigit</span><span class="token punctuation">(</span> <span class="token keyword">int</span> ch <span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">int</span> <span class="token function">isxdigit</span><span class="token punctuation">(</span><span class="token keyword">int</span> ch<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//若ch是16进制数('0'-'9','A'-'F','a'-'f')返回非0值, 否则返回0</span>

<span class="token comment">//如果是'a'-'z'返回对应的ASCII码，否则返回0『表示不是』</span>
<span class="token keyword">int</span> <span class="token function">islower</span><span class="token punctuation">(</span> <span class="token keyword">int</span> ch <span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">//如果是'A'-'Z'返回对应的ASCII码，否则返回0『表示不是』</span>
<span class="token keyword">int</span> <span class="token function">isupper</span><span class="token punctuation">(</span> <span class="token keyword">int</span> ch <span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token comment">//如果是'a'-'z'或者'A'-'Z'，返回对应的ASCII码，否则返回0『表示不是』</span>
<span class="token keyword">int</span> <span class="token function">isalpha</span><span class="token punctuation">(</span> <span class="token keyword">int</span> ch <span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token comment">//单词来源：alnum= alpha+num</span>
<span class="token comment">//如果是'0'-'9'或'a'-'z'或者'A'-'Z'，返回对应的ASCII码，否则返回0『表示不是』</span>
<span class="token keyword">int</span> <span class="token function">isalnum</span><span class="token punctuation">(</span> <span class="token keyword">int</span> ch <span class="token punctuation">)</span><span class="token punctuation">;</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>转换类</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//如果是'A'-'Z'返回对应的'a'-'z'，否则返回它本身『也就是不变化』</span>
<span class="token keyword">int</span> <span class="token function">tolower</span><span class="token punctuation">(</span> <span class="token keyword">int</span> ch <span class="token punctuation">)</span><span class="token punctuation">;</span> 

<span class="token keyword">int</span> <span class="token function">toupper</span><span class="token punctuation">(</span> <span class="token keyword">int</span> ch <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//小写转大写</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>测试上面代码</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">//测试代码</span>
<span class="token keyword">char</span> a<span class="token operator">=</span><span class="token string">'A'</span><span class="token punctuation">;</span>
<span class="token keyword">char</span> b<span class="token operator">=</span><span class="token string">'+'</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%c %c\n&quot;</span><span class="token punctuation">,</span> <span class="token function">tolower</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">tolower</span><span class="token punctuation">(</span>b<span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_2-2-字符串比较『记忆法』"><a href="#_2-2-字符串比较『记忆法』" class="header-anchor">#</a> 2.2.字符串比较『记忆法』</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>记忆方法：类比数学
<span class="token function">strcmp</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token operator">&gt;</span><span class="token number">0</span>  表示a<span class="token operator">-</span>b<span class="token operator">&gt;</span><span class="token number">0</span>  也就是，表示a比b的字典序大
<span class="token function">strcmp</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token number">0</span>  表示a<span class="token operator">-</span>b<span class="token operator">&lt;</span><span class="token number">0</span>  也就是，表示a比b的字典序小
<span class="token function">strcmp</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>b<span class="token punctuation">)</span><span class="token operator">==</span><span class="token number">0</span>  表示a<span class="token operator">-</span>b<span class="token operator">=</span><span class="token number">0</span>  也就是，表示a和b相同
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h3 id="_2-3-记忆g-中的宏✔️"><a href="#_2-3-记忆g-中的宏✔️" class="header-anchor">#</a> 2.3.记忆<code>g++</code>中的宏✔️</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">int</span> a<span class="token operator">=</span>INT_MAX<span class="token punctuation">;</span>
    <span class="token comment">//long long 最大值</span>
	cout<span class="token operator">&lt;&lt;</span>LONG_LONG_MAX<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
    
    <span class="token comment">//unsigned long long的最大值</span>
	cout <span class="token operator">&lt;&lt;</span> ULONG_LONG_MAX <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
    
    <span class="token comment">//用signed输出，会溢出，发现是-1</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%lld\n&quot;</span><span class="token punctuation">,</span>ULONG_LONG_MAX<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//用unsigned的输出就不会溢出</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%llu\n&quot;</span><span class="token punctuation">,</span> ULONG_LONG_MAX<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%u\n&quot;</span><span class="token punctuation">,</span> INT_MAX<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><ul><li>第2组测试</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	cout<span class="token operator">&lt;&lt;</span>INT_MIN<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span><span class="token number">0x80000000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	cout<span class="token operator">&lt;&lt;</span><span class="token number">0x80000000</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>	<span class="token comment">//注意：这个将溢出的进行了转型</span>

	cout<span class="token operator">&lt;&lt;</span>INT_MAX<span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	cout<span class="token operator">&lt;&lt;</span><span class="token number">0x7fffffff</span><span class="token operator">&lt;&lt;</span>endl<span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">/*
-2147483648
-2147483648
2147483648
2147483647
2147483647
/
</span></code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_2-4-常见数据表示的范围"><a href="#_2-4-常见数据表示的范围" class="header-anchor">#</a> 2.4.常见数据表示的范围</h3> <ul><li>long long的最大值：9223372036854775807</li> <li>long long的最小值：-9223372036854775808</li> <li>unsigned long long的最大值：18446744073709551615</li> <li><code>__int64</code>的最大值：9223372036854775807</li> <li><code>__int64</code>的最小值：-9223372036854775808</li> <li><code>unsigned __int64</code>的最大值：18446744073709551615</li></ul> <blockquote><p>最大值的优缺点：</p></blockquote> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>对于无穷大的初始化：
<span class="token keyword">const</span> <span class="token keyword">int</span> inf <span class="token operator">=</span> <span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> INF <span class="token operator">=</span> <span class="token number">0x7fffffff</span><span class="token punctuation">;</span>

<span class="token function">memset</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> inf<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//正确</span>
<span class="token function">memset</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> INF<span class="token punctuation">,</span> <span class="token function">sieof</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//错误</span>

对于<span class="token keyword">int</span>型数组a可以用<span class="token number">0x3f3f3f3f</span>初始化，但不可以用<span class="token number">0x7fffffff</span>，如果非要把a数组初始化为<span class="token number">0x7ffffff</span>，可以用语句 <span class="token function">fill</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span> a<span class="token operator">+</span>n<span class="token punctuation">,</span> INF<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//n为元素个数</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="_2-4-long-long和double在icpc中"><a href="#_2-4-long-long和double在icpc中" class="header-anchor">#</a> 2.4.<code>long long</code>和<code>double</code>在ICPC中</h3> <ul><li>样题：<a href="https://ac.nowcoder.com/acm/contest/338/G" target="_blank" rel="noopener noreferrer">a+b+c+d<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <h3 id="常见错误答案"><a href="#常见错误答案" class="header-anchor">#</a> 常见错误答案</h3> <ul><li>错在：范围是可能等于2<sup>61</sup>的，那么4个long long相加会超过</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">int</span> n<span class="token punctuation">;</span>
	<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">)</span> <span class="token punctuation">;</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span> n<span class="token operator">--</span> <span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			<span class="token keyword">long</span> <span class="token keyword">long</span> a<span class="token punctuation">,</span>b<span class="token punctuation">,</span>c<span class="token punctuation">,</span>d<span class="token punctuation">;</span>
			<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%lld%lld%lld%lld&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>a<span class="token punctuation">,</span><span class="token operator">&amp;</span>b<span class="token punctuation">,</span><span class="token operator">&amp;</span>c<span class="token punctuation">,</span><span class="token operator">&amp;</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%lld\n&quot;</span><span class="token punctuation">,</span> a<span class="token operator">+</span>b<span class="token operator">+</span>c<span class="token operator">+</span>d <span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h3 id="解决方案"><a href="#解决方案" class="header-anchor">#</a> 解决方案</h3> <ul><li>原因是double能承载更多『记住这个小技巧』</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> 
<span class="token punctuation">{</span>
    <span class="token keyword">int</span> T<span class="token punctuation">;</span>
    <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%d&quot;</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>T<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">while</span><span class="token punctuation">(</span>T<span class="token operator">--</span><span class="token punctuation">)</span> 
    <span class="token punctuation">{</span>
        <span class="token keyword">double</span> a<span class="token punctuation">,</span> b<span class="token punctuation">,</span> c<span class="token punctuation">,</span> d<span class="token punctuation">;</span>
        <span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%lf%lf%lf%lf&quot;</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>a<span class="token punctuation">,</span> <span class="token operator">&amp;</span>b<span class="token punctuation">,</span> <span class="token operator">&amp;</span>c<span class="token punctuation">,</span> <span class="token operator">&amp;</span>d<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">double</span> sum<span class="token operator">=</span>a<span class="token operator">+</span>b<span class="token operator">+</span>c<span class="token operator">+</span>d<span class="token punctuation">;</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%.0lf\n&quot;</span><span class="token punctuation">,</span> sum<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="_2-5-✔️位运算实现大小写转换"><a href="#_2-5-✔️位运算实现大小写转换" class="header-anchor">#</a> 2.5.✔️位运算实现大小写转换</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token string">'a'</span><span class="token operator">^</span><span class="token string">' '</span><span class="token operator">==</span><span class="token string">'A'</span><span class="token punctuation">;</span>	<span class="token comment">//'a'^32=='A'</span>
<span class="token string">'A'</span><span class="token operator">^</span><span class="token string">' '</span><span class="token operator">==</span><span class="token string">'a'</span><span class="token punctuation">;</span>	<span class="token comment">//'A'^32=='a'</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="⭐️输入输出提升"><a href="#⭐️输入输出提升" class="header-anchor">#</a> ⭐️输入输出提升</h2> <h3 id="_3-1-cin和scanf速度相关"><a href="#_3-1-cin和scanf速度相关" class="header-anchor">#</a> 3.1.cin和scanf速度相关</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>sync_with_stdio  site<span class="token operator">:</span> https<span class="token operator">:</span><span class="token comment">//blog.nowcoder.net/hacv</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">lson</span> <span class="token expression">rt<span class="token operator">&lt;&lt;</span><span class="token number">1</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">rson</span> <span class="token expression">rt<span class="token operator">&lt;&lt;</span><span class="token number">1</span><span class="token operator">|</span><span class="token number">1</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">lowbit</span><span class="token expression"><span class="token punctuation">(</span>x<span class="token punctuation">)</span> x<span class="token operator">&amp;</span><span class="token punctuation">(</span><span class="token operator">-</span>x<span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">name2str</span><span class="token expression"><span class="token punctuation">(</span>name<span class="token punctuation">)</span> <span class="token punctuation">(</span>#name<span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">bug</span> <span class="token expression"><span class="token function">printf</span><span class="token punctuation">(</span></span><span class="token string">&quot;*********\n&quot;</span><span class="token expression"><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name function">debug</span><span class="token expression"><span class="token punctuation">(</span>x<span class="token punctuation">)</span> cout<span class="token operator">&lt;&lt;</span>#x</span><span class="token string">&quot;=[&quot;</span><span class="token expression"><span class="token operator">&lt;&lt;</span>x<span class="token operator">&lt;&lt;</span></span><span class="token string">&quot;]&quot;</span> <span class="token expression"><span class="token operator">&lt;&lt;</span>endl</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">FIN</span> <span class="token expression"><span class="token function">freopen</span><span class="token punctuation">(</span></span><span class="token string">&quot;&quot;</span><span class="token expression"><span class="token punctuation">,</span></span><span class="token string">&quot;r&quot;</span><span class="token expression"><span class="token punctuation">,</span><span class="token constant">stdin</span><span class="token punctuation">)</span></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">define</span> <span class="token macro-name">IO</span> <span class="token expression">ios<span class="token operator">::</span><span class="token function">sync_with_stdio</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">,</span>cin<span class="token punctuation">.</span><span class="token function">tie</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span></span></span>



ios<span class="token operator">::</span><span class="token function">sync_with_stdio</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
cin<span class="token punctuation">.</span><span class="token function">tie</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
cout<span class="token punctuation">.</span><span class="token function">tie</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
在ACM里，经常出现数据集超大造成 cin TLE的情况。这时候大部分人认为这是cin的效率不及scanf的错，甚至还上升到C语言和C<span class="token operator">++</span>语言的执行效率层面的无聊争论。
    其实像上文所说，这只是C<span class="token operator">++</span>为了兼容而采取的保守措施。我们可以在IO之前将stdio解除绑定，这样做了之后要注意不要同时混用cout和printf之类。

在默认的情况下cin绑定的是cout，每次执行 <span class="token operator">&lt;&lt;</span> 操作符的时候都要调用flush，这样会增加IO负担。可以通过<span class="token function">tie</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span>（<span class="token number">0</span>表示<span class="token constant">NULL</span>）来解除cin与cout的绑定，进一步加快执行效率。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><h3 id="_3-2-getchar设计read函数快速读入（正负）"><a href="#_3-2-getchar设计read函数快速读入（正负）" class="header-anchor">#</a> 3.2.<code>getchar</code>设计read函数快速读入（正负）</h3> <ul><li>即输入字符转换成数字，效率高，在输入数据量特别大的时候采用快速读入可以『避免超时』</li> <li>Q：为什么要写快读，cin，scanf不是很简单吗，难道这很慢吗，真的！（之前Pascal就无所谓，全部都read/readln）</li> <li>A：我们知道读入字符却是很快的(即getchar)，那么很显然，我们可以用读入字符转数字来加快读入</li> <li>A：写成inline函数是为了加速读入『C++技巧』</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;bits/stdc++.h&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">inline</span> <span class="token keyword">int</span> <span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token comment">//res表示数字本身,NumSign代表正负,最后乘上res就可以了。</span>
	<span class="token keyword">int</span> res<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> NumSign<span class="token operator">=</span><span class="token number">1</span><span class="token punctuation">;</span>
	<span class="token keyword">char</span> c<span class="token operator">=</span><span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token comment">//c如果是非数字字符</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span> c<span class="token operator">&lt;</span><span class="token string">'0'</span><span class="token operator">||</span>c<span class="token operator">&gt;</span><span class="token string">'9'</span> <span class="token punctuation">)</span> 
	<span class="token punctuation">{</span>
		<span class="token comment">//如果c是负号就把NumSign赋为-1</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span> <span class="token string">'-'</span><span class="token operator">==</span>c <span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			NumSign<span class="token operator">=</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		c<span class="token operator">=</span><span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">while</span><span class="token punctuation">(</span> c<span class="token operator">&gt;=</span><span class="token string">'0'</span><span class="token operator">&amp;&amp;</span>c<span class="token operator">&lt;=</span><span class="token string">'9'</span> <span class="token punctuation">)</span> 
	<span class="token punctuation">{</span>
		res<span class="token operator">=</span> res<span class="token operator">*</span><span class="token number">10</span> <span class="token operator">+</span> c<span class="token operator">-</span><span class="token string">'0'</span><span class="token punctuation">;</span>
		c<span class="token operator">=</span><span class="token function">getchar</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	
	<span class="token keyword">return</span> NumSign<span class="token operator">*</span>res<span class="token punctuation">;</span><span class="token comment">//乘起来输出</span>
<span class="token punctuation">}</span>


<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">int</span> n<span class="token punctuation">;</span>
	n<span class="token operator">=</span><span class="token function">read</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token keyword">if</span><span class="token punctuation">(</span> n<span class="token operator">&lt;=</span><span class="token number">0</span> <span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;num=%d\n&quot;</span><span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;pause&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">int</span> solve<span class="token punctuation">[</span>n<span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">int</span> loop<span class="token operator">=</span>n<span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span> loop<span class="token operator">--</span> <span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>solve<span class="token punctuation">[</span>loop<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>

	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>n<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d%c&quot;</span><span class="token punctuation">,</span>solve<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> i<span class="token operator">!=</span><span class="token punctuation">(</span>n<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">' '</span> <span class="token operator">:</span> <span class="token string">'\n'</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	
	<span class="token function">system</span><span class="token punctuation">(</span><span class="token string">&quot;pause&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br></div></div><h4 id="『关于getchar的轶事』"><a href="#『关于getchar的轶事』" class="header-anchor">#</a> 『关于getchar的轶事』</h4> <ul><li><p>有人说：<code>getchar()</code>的作用，某大学ICPC有人，每次输入输出用<code>getchar()</code>，从来不用cin和scanf，那个要很久。能够省略0.4s，那次就竟然，就AC了，那次是<strong>卡的常数</strong>。那次，卡点，AC了</p></li> <li><p>个人观点：</p> <ul><li>这些技巧学学就好了，算法才是硬实力，算法学习不能太推崇对这些这种奇技淫巧的喜爱！</li> <li>编码技巧不应该是算法课程的注重点。</li> <li>我们应该把重点在于如何提高我们的算法能力！</li> <li>那些技巧，能学会也好，但是不要太依赖那些！！</li></ul></li></ul> <h4 id="『icpc轶事』"><a href="#『icpc轶事』" class="header-anchor">#</a> 『ICPC轶事』</h4> <ul><li>印度尼西亚赛区，出现过一上来就AC，10分钟一道万能程序：</li> <li>据说原理是：『多次提交，读到答案』</li></ul> <h2 id="⭐️类型转换记忆"><a href="#⭐️类型转换记忆" class="header-anchor">#</a> ⭐️类型转换记忆</h2> <h3 id="_4-1-函数to-string-将数字转换为string『⭐️c-11』"><a href="#_4-1-函数to-string-将数字转换为string『⭐️c-11』" class="header-anchor">#</a> 4.1.函数<code>to_string</code> 将数字转换为string『⭐️C++11』</h3> <ul><li><p>注意：<code>to_string</code>函数是C++11才提供的！</p></li> <li><p><a href="https://leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/" target="_blank" rel="noopener noreferrer">LeetCode剑指 Offer 45. 把数组排成最小的数<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></p></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">bool</span> <span class="token function">cmp</span><span class="token punctuation">(</span>string s1 <span class="token punctuation">,</span> string s2<span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>s1<span class="token operator">+</span>s2<span class="token punctuation">)</span><span class="token operator">&lt;</span><span class="token punctuation">(</span>s2<span class="token operator">+</span>s1<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    string <span class="token function">minNumber</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span><span class="token operator">&amp;</span> nums<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        vector<span class="token operator">&lt;</span>string<span class="token operator">&gt;</span> solve<span class="token punctuation">;</span>
        <span class="token keyword">int</span> len<span class="token operator">=</span>nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>len<span class="token punctuation">;</span> <span class="token operator">++</span>i <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
			<span class="token comment">//此处使用了</span>
            solve<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span> <span class="token function">to_string</span><span class="token punctuation">(</span> nums<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token function">sort</span><span class="token punctuation">(</span> solve<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> solve<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> cmp<span class="token punctuation">)</span><span class="token punctuation">;</span>
        string rt<span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span>len<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            rt<span class="token operator">+=</span>solve<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> rt<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><h3 id="_4-2-string→int-long-long-double等"><a href="#_4-2-string→int-long-long-double等" class="header-anchor">#</a> 4.2.<code>string→int/long long /double</code>等</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>string str<span class="token operator">=</span><span class="token string">&quot;123.2&quot;</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> x<span class="token operator">=</span><span class="token function">stoi</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">long</span> x<span class="token operator">=</span><span class="token function">stol</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">long</span> <span class="token keyword">long</span> x<span class="token operator">=</span><span class="token function">stoll</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">float</span> x<span class="token operator">=</span><span class="token function">stof</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">double</span> x<span class="token operator">=</span><span class="token function">stod</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">long</span> <span class="token keyword">double</span> x<span class="token operator">=</span><span class="token function">stold</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><ul><li>参考自：《C++程序设计语言》第4部分：标准库，P154</li></ul> <h2 id="⭐️sscanf-sprintf的使用和记忆方法"><a href="#⭐️sscanf-sprintf的使用和记忆方法" class="header-anchor">#</a> ⭐️sscanf/sprintf的使用和记忆方法</h2> <ul><li>记忆方法：借助，<code>scanf</code>和<code>sprintf</code>的使用</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">long</span> <span class="token keyword">long</span>  n<span class="token punctuation">;</span>
<span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%lld&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%lld&quot;</span><span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>注意，scanf和printf的『<strong>尾部不变</strong>』之后，修改为（重点记忆）</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">char</span> str<span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">long</span> <span class="token keyword">long</span> n<span class="token punctuation">;</span>
<span class="token function">sscanf</span><span class="token punctuation">(</span>  str  <span class="token punctuation">,</span><span class="token string">&quot;%lld&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//尾部2个不变</span>
<span class="token function">sprintf</span><span class="token punctuation">(</span> str  <span class="token punctuation">,</span><span class="token string">&quot;%lld&quot;</span><span class="token punctuation">,</span>n<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//尾部2个不变</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h4 id="_1、综述"><a href="#_1、综述" class="header-anchor">#</a> 1、综述</h4> <ul><li>正则表达式Regex(regular expression)是一种强大的描述字符序列的工具。在许多语言中都存在着正则表达式.</li> <li>sscanf可以用来实现一些简易的类正则表达式功能，提高对字符串的处理效率，但却没有正则表达式强大,所以如果对于比较复杂的字符串处理,建议使用正则表达式。</li> <li>注：C语言中sscanf实现的只是的<b>类正则表达式功能</b>，并不是真的支持正则表达式</li></ul> <blockquote><ul><li>C++语言中，也只有C++11的时候，才将正则表达式纳入了新标准的一部分</li> <li>不仅如此，它还支持了6种不同的正则表达式的语法，分别是：ECMASCRIPT、basic、extended、awk、grep和egrep。其中ECMASCRIPT是默认的语法，具体使用哪种语法我们可以在构造正则表达式的时候指定。（本段文字还未考证）</li> <li>C++11中使用正则表达式，需要include头文件<b>（regex.h）</b></li> <li>在DevC++中编译器默认没有开C++11标准的编译，所以显示没有这个头文件。（但你可以在DevC++中自行启用编译选项，加入<b>（-std=c++11）</b>用C++11就有了）</li></ul></blockquote> <ul><li>《C和指针》一书中描述P308</li> <li>scanf函数家族的<strong>格式代码</strong>都以一个百分号开头，后面可以是：
<ul><li>（1）一个可选的星号（<code>'*'</code>）</li> <li>（2）一个可选的宽度（<code>scanf(&quot;%4s&quot;,buff)</code>）</li> <li>（3）一个可选的限定符</li> <li>（4）格式代码</li></ul></li></ul> <h4 id="_2、sscanf的用法"><a href="#_2、sscanf的用法" class="header-anchor">#</a> 2、sscanf的用法</h4> <h5 id="_1、跳过数据"><a href="#_1、跳过数据" class="header-anchor">#</a> 1、跳过数据</h5> <blockquote><ul><li>语法：</li></ul> <blockquote><ul><li><b>跳过的话，就是加一个*</b>，然后就可以跳过*后面的那种</li> <li>比如，%*s或%*d</li></ul></blockquote></blockquote> <div class="language-c line-numbers-mode"><pre class="language-c"><code>例<span class="token operator">:</span> <span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;1234 5678&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;%*d %s&quot;</span><span class="token punctuation">,</span> buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">//这里的d和后面的%之间有个空格</span>
<span class="token comment">//%d的话就是要一个整数，而&lt;b&gt;%*d就是将整数跳过&lt;/b&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>例子：</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> buff<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;1234 5678&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%*d %s&quot;</span><span class="token punctuation">,</span>buff<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//跳过1234，然后隔一个空格再获取字符串</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s&quot;</span><span class="token punctuation">,</span>buff<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//输出为：</span>
<span class="token comment">//5678</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h5 id="_2、读指定宽度的数据"><a href="#_2、读指定宽度的数据" class="header-anchor">#</a> 2、读指定宽度的数据</h5> <blockquote><ul><li>语法：</li></ul> <blockquote><ul><li>比如，<b>%[width]s</b> ，<b>%[width]d</b>  其中，[]表示的是<b>可选</b>（也就是可以省略的意思）</li> <li>例: sscanf(&quot;12345678&quot;, &quot;%4s&quot;, buf);</li></ul></blockquote></blockquote> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//例1 :</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;12345678&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%4s&quot;</span><span class="token punctuation">,</span> buf<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//从字符串中获取字符串，只要4个字节，存放在buf中</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span> 
<span class="token punctuation">}</span> 
<span class="token comment">//输出：</span>
<span class="token comment">//1234</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//例2 :</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">int</span> num<span class="token punctuation">;</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;12345678&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%3d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span> <span class="token punctuation">;</span>
	
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//输出的：</span>
<span class="token comment">//123</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//例3 :(综合1和2)</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">int</span> num<span class="token punctuation">;</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;12345678&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%*3d%3d&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span> <span class="token punctuation">;</span>
	
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 
<span class="token comment">//输出：</span>
<span class="token comment">//456</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h5 id="_3、支持集合操作"><a href="#_3、支持集合操作" class="header-anchor">#</a> 3、支持集合操作</h5> <p><font style="background:yellow;">注意：只支持获取到字符串中去，不能获取到整数中去！</font></p> <blockquote><ul><li>语法：</li></ul> <blockquote><ul><li>%[a-z]表示匹配a到z中任意字符(具有贪婪性——尽可能多的匹配)</li> <li>%[^a-z]表示读取除a-z<b>以外</b>的所有字符,其实，你可以a-z也可以b-f，他是一个ASCII码范围。</li> <li>%[aBc]匹配a、B、c中这几个列出了的字符中的，贪婪性</li> <li>%[^aFc]匹配<b>非</b>aFc的任意字符，贪婪性</li></ul></blockquote></blockquote> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//例1：</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span> <span class="token punctuation">;</span>
	<span class="token function">sscanf</span> <span class="token punctuation">(</span><span class="token string">&quot;agkd32DajfDdFF&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;%[a-z]&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//从字符串中获取输入只要'a''g''k''d'</span>
	<span class="token function">printf</span> <span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span> <span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//结果为agkd</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//例2：(综合1和3)</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span> <span class="token punctuation">;</span>
	<span class="token function">sscanf</span> <span class="token punctuation">(</span><span class="token string">&quot;agkd32DajfDdFF&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;%*[a-z]%[^a-z]&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//先跳过，然后要ASCII码不是a-z区间的字符	 </span>
	<span class="token function">printf</span> <span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span> <span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//结果为32D</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//例3：(综合2和3)</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span> <span class="token punctuation">;</span>
	<span class="token function">sscanf</span> <span class="token punctuation">(</span><span class="token string">&quot;agkd32DajfDdFF&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%*[a-z]%5s&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//先跳过，然后扫描进5个字节的字符</span>
	<span class="token function">printf</span> <span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span> <span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//结果为32Daj </span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><font color="red" size="4">本样例，需注意</font></p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//例4：</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token comment">//一旦碰到我不要的，后面的也不看</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;ASHJ$12agkd32DajfDdFF&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%[^a-z]&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//结果为ASHJ$12</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//例5：</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;acsfsacssss&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%[absc]&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//结果为acs</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><font color="red" size="4">易错样例：</font></p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span> <span class="token punctuation">;</span>
	
	<span class="token function">sscanf</span> <span class="token punctuation">(</span><span class="token string">&quot;32DajfDdFF&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;%[1-9]&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//先跳过，然后要ASCII码不是a-z区间的	 </span>
	<span class="token function">printf</span> <span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>buf<span class="token punctuation">)</span> <span class="token punctuation">;</span>
	<span class="token comment">//输入32</span>
	 
	<span class="token keyword">int</span> num<span class="token punctuation">;</span> 
	<span class="token function">sscanf</span> <span class="token punctuation">(</span><span class="token string">&quot;32DajfDdFF&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;%[1-9]&quot;</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//先跳过，然后要ASCII码不是a-z区间的	 </span>
	<span class="token function">printf</span> <span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span> <span class="token punctuation">;</span>
	<span class="token comment">// 出错，不支持，扫描到数字中去！！！</span>
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h4 id="_3、深刻理解"><a href="#_3、深刻理解" class="header-anchor">#</a> 3、深刻理解</h4> <ul><li>借此来深刻理解，为什么以前我们要记忆那么多所谓的格式字符了</li> <li>sscanf和scanf和printf的联系</li></ul> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">int</span> num<span class="token punctuation">;</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;AAA123BBB456&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%*[^0-9]%i&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token comment">//先跳过，然后扫描整数  </span>
	<span class="token comment">//其实上面一句等价于，sscanf(&quot;AAA123BBB456&quot;,&quot;%*[^0-9]%d&quot;,&amp;num); </span>
	<span class="token comment">//也就是%d和%i都是十进制整数的格式字符，我之所以在这用的是%i</span>
	<span class="token comment">//是为了将正则表达式中类似的语法导入进来，i（integer整数） </span>
	<span class="token comment">//此外，我们可以发现，其实我们在scanf扫入和printf输出的时候，也都用了格式字符 </span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//结果为123</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> c<span class="token punctuation">;</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span><span class="token string">&quot;AAA123BBB456&quot;</span><span class="token punctuation">,</span><span class="token string">&quot;%*[^0-9]%c&quot;</span><span class="token punctuation">,</span><span class="token operator">&amp;</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span> 
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%c\n&quot;</span><span class="token punctuation">,</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//结果为1</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">//补充例子</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span>  <span class="token operator">*</span>line<span class="token operator">=</span><span class="token string">&quot;6161665058&quot;</span><span class="token punctuation">;</span>
	<span class="token keyword">char</span> buff<span class="token punctuation">[</span><span class="token number">20</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token function">sscanf</span><span class="token punctuation">(</span>line<span class="token punctuation">,</span><span class="token string">&quot;%2[{0|1|6}]&quot;</span><span class="token punctuation">,</span>buff<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>buff<span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//结果为61</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><ul><li>（此处还需补充更好的例子）</li></ul> <h2 id="✔️memset和fill"><a href="#✔️memset和fill" class="header-anchor">#</a> ✔️memset和fill</h2> <h4 id="_1-给数组赋相同值—memset"><a href="#_1-给数组赋相同值—memset" class="header-anchor">#</a> 1.给数组赋相同值—Memset</h4> <ul><li>C语言:头文件<code>string.h</code></li> <li>C++：头文件<code>cstring</code></li></ul> <h5 id="_1-1-memset对数组中每个元素赋值"><a href="#_1-1-memset对数组中每个元素赋值" class="header-anchor">#</a> 1.1.memset对数组中每个元素赋值</h5> <h5 id="⭐️devc-中memset源代码的声明"><a href="#⭐️devc-中memset源代码的声明" class="header-anchor">#</a> ⭐️DevC++中memset源代码的声明</h5> <p><code>void * __cdecl memset(void *_Dst,int _Val,size_t _Size);</code></p> <ul><li>所以常用下面的</li></ul> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//一维数组</span>
<span class="token function">memset</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//二维数组</span>
<span class="token function">memset</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">memset</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">0x3f</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//给int的数字赋值0x3f3f3f3f</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> a<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//一维字符数组。</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token number">65</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">10</span> <span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> j<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>j<span class="token operator">&lt;</span><span class="token number">10</span> <span class="token punctuation">;</span>j<span class="token operator">++</span><span class="token punctuation">)</span>
		<span class="token punctuation">{</span>
			<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%c %d\n&quot;</span><span class="token punctuation">,</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">,</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
		
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//测试结果是循环输出A 65</span>
<span class="token comment">//原因，65的二进制`0100` `0001‬，它的最后一个字节是也正好是这个  </span>
<span class="token comment">//然后，我要重复使用这个字节填入char，也正好一个字节，那就正好是它了  </span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h5 id="_1-2-memset底层原理⭐️"><a href="#_1-2-memset底层原理⭐️" class="header-anchor">#</a> 1.2.<code>memset</code>底层原理⭐️</h5> <p>原理：<font size="3">取二进制位的<strong>最后一个字节</strong></font>，<font style="background:yellow;"><strong>重复使用</strong></font>它给目标赋值。</p> <p>验证上述的例子：</p> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;stdio.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token function">memset</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1023</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>a<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">10</span> <span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>	
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%d\n&quot;</span><span class="token punctuation">,</span>a<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	
	<span class="token punctuation">}</span>
		
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">////测试结果是循环输出16843009</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><p>解释，首先我们看一下**-1023**用int类型表示的补码是</p> <ul><li>‭<code>1111 1111</code> <code>1111 1111</code> <code>1111 1100</code> <code>0000 0001</code>‬</li> <li>发现，<strong>最后一个字节</strong>的bits是<code>0000 0001</code>‬</li> <li>我们将这个字节<strong>重复使用</strong>，放到一个<strong>int类型</strong>中去，也就组成了</li> <li><code>0000 0001</code>‬ <code>0000 0001</code>‬ <code>0000 0001</code>‬ <code>0000 0001</code>‬</li> <li>然后，我们将这个二进制化为十进制发现，是这样的，正好等于<strong>16843009</strong></li></ul> <h5 id="_1-3-解释一些说法"><a href="#_1-3-解释一些说法" class="header-anchor">#</a> 1.3.解释一些说法</h5> <ul><li>正是由于，只看那个最后一个字节。</li> <li>所以，我们在编码的时候，常用用这个初始化全0，或者全-1</li> <li>因为，他们的补码最后一个字节分别是<code>0000 0000</code> 和 <code>1111 1111</code></li> <li>而其他的的最后一个字节的重复使用，对于赋值给不同类型的数据，效果不同，也就比较少用了。
<ul><li>PS:个人习惯的话，看自己喜欢吧，以前写俄罗斯方块，最初用的数组，后面学了位运算，发现还能这样精确的控制bit，我还优化了一次。（虽说，那样的思维强度比直观的用数组高一点）<br>
当然，要是从<strong>可读性</strong>方面来看，还是数组的好一点，位运算的，可能对看自己代码的不友好。</li> <li>此外，memset比<code>C++中fill</code>的速度快，所以，做OJ的一些题目，能用这种提速就尽量用这种。</li></ul></li></ul> <h4 id="_2-给数组赋相同值—fill"><a href="#_2-给数组赋相同值—fill" class="header-anchor">#</a> 2.给数组赋相同值—fill</h4> <ul><li>C++中头文件<code>#include&lt;algorithm&gt;</code></li> <li>fill可以将<strong>数组</strong>（我记得我看过的有一本书，似乎上面说，数组其实在<code>C++</code>中也是一容器（北大郭炜的书中写的）  或<strong>容器</strong>中的某一段区间赋为某个相同的值。</li></ul> <h5 id="_2-1-fill的常用用法"><a href="#_2-1-fill的常用用法" class="header-anchor">#</a> 2.1.fill的常用用法</h5> <ul><li>fill的源代码</li></ul> <h5 id="⭐️devc-中fill源代码的声明"><a href="#⭐️devc-中fill源代码的声明" class="header-anchor">#</a> ⭐️DevC++中fill源代码的声明</h5> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">template</span><span class="token operator">&lt;</span><span class="token keyword">typename</span> <span class="token class-name">_FIter</span><span class="token punctuation">,</span> <span class="token keyword">typename</span> <span class="token class-name">_Tp</span><span class="token operator">&gt;</span>
<span class="token keyword">void</span>  <span class="token function">fill</span><span class="token punctuation">(</span>_FIter<span class="token punctuation">,</span> _FIter<span class="token punctuation">,</span> <span class="token keyword">const</span> _Tp<span class="token operator">&amp;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>和memset不同，这下你想给那个数组赋值是多少就可以<strong>任意</strong>选，你完全不用从bit的角度来思考很直观。</p> <ul><li>显然，这个模板函数，和其他模板函数比如<code>sort</code>一样，使用的迭代器
<ul><li>而，对于int数组，没有迭代器，就使用指针</li></ul></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">int</span> a<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//一维数组</span>
<span class="token function">fill</span><span class="token punctuation">(</span>a<span class="token punctuation">,</span>a<span class="token operator">+</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">233</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>OJ中，做图论等一些题目，用<strong>邻接矩阵</strong>常用下面的方式</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">const</span> <span class="token keyword">int</span> INF<span class="token operator">=</span><span class="token number">0x3f3f3f3f</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">int</span> MAXN<span class="token operator">=</span><span class="token number">200</span><span class="token punctuation">;</span>
<span class="token keyword">int</span> a<span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">[</span>MAXN<span class="token punctuation">]</span><span class="token punctuation">;</span><span class="token comment">//二维数组</span>
<span class="token function">fill</span><span class="token punctuation">(</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">,</span>a<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">+</span>MAXN<span class="token operator">*</span>MAXN<span class="token punctuation">,</span>INF<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//注意这里a[0]</span>

<span class="token comment">//对于STL中的fill的使用是『现在，一切都知道了』</span>
vector<span class="token operator">&lt;</span><span class="token keyword">int</span><span class="token operator">&gt;</span> <span class="token function">help</span><span class="token punctuation">(</span> nums<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fill</span><span class="token punctuation">(</span> help<span class="token punctuation">.</span><span class="token function">begin</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">,</span> help<span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><ul><li><p>十六进制0x3f3f3f3f</p></li> <li><p>对应二进制</p></li> <li><p>‭<code>0011 1111</code> <code>0011 1111</code> <code>0011 1111</code> <code>0011 1111‬</code></p></li> <li><p>Tips:看二进制，个人还是习惯直接用Windows的<code>calc</code></p></li></ul> <h2 id="⭐️c-中string⇔c语言风格的字符数组⇔char"><a href="#⭐️c-中string⇔c语言风格的字符数组⇔char" class="header-anchor">#</a> ⭐️C++中<code>string</code>⇔C语言风格的<code>字符数组</code>⇔<code>char</code></h2> <ul><li><p>快速转换『两种方式』</p> <ul><li>受到C语言中<code>sscanf</code>和<code>sprintf</code>的启发</li></ul></li></ul> <h3 id="_5-1-string→字符数组"><a href="#_5-1-string→字符数组" class="header-anchor">#</a> 5.1.string→字符数组</h3> <ul><li><strong>方法1、用string类的成员函数<code>copy</code></strong></li> <li>注意，其实，C语言输入输出和C++输入输出，最好不要混合输入输出，我这样写只是为了论证这样可以</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span> </span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cstring&gt;</span> </span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		string str<span class="token punctuation">;</span>
		cin<span class="token operator">&gt;&gt;</span>str<span class="token punctuation">;</span>
		<span class="token keyword">char</span> test<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 
		<span class="token function">memset</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>test<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">//上面的memset是我故意，用来让自己注意</span>
		<span class="token comment">//string转换到字符数组后，末尾的'\0'要手动加，不然就不行</span>
		str<span class="token punctuation">.</span><span class="token function">copy</span><span class="token punctuation">(</span>  test <span class="token punctuation">,</span>  str<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token punctuation">)</span><span class="token punctuation">;</span>
		test<span class="token punctuation">[</span>str<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">'\0'</span><span class="token punctuation">;</span>
		 
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>test<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span> 
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><ul><li><strong>方法2、string的<code>c_str()</code>和C语言中的<code>strcpy</code></strong></li> <li><font size="3" style="background:yellow;">重点：返回的是“const char*”，不是什么string类</font></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span> </span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cstring&gt;</span> </span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		string str<span class="token punctuation">;</span>
		cin<span class="token operator">&gt;&gt;</span>str<span class="token punctuation">;</span>
		<span class="token keyword">char</span> test<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span> 
		<span class="token function">memset</span><span class="token punctuation">(</span>test<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token keyword">sizeof</span><span class="token punctuation">(</span>test<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">//上面的memset是我故意，告诉我们这样就不用管这些 </span>
		
		<span class="token function">strcpy</span><span class="token punctuation">(</span>  test<span class="token punctuation">,</span>  str<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token punctuation">)</span><span class="token punctuation">;</span> 
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>test<span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span> 
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>cplusplus.com上的声明如下</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>const char* c_str() const;	//返回一个以'\0'结尾的字符串的首地址
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><div class="language-txt line-numbers-mode"><pre class="language-text"><code>Get C string equivalent
Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character ('\0') at the end.
翻译：该数组包括组成字符串对象值的相同字符序列，最后还有一个额外的终止空字符（'\ 0'）
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p><font size="3" style="background:red;">从说明来看，我们使用这个进行string到字符数组的转换，我们就不要担心在字符数组后有没有加'\0'了，而如果用string类的成员函数copy()来进行转换，它却没有给我们在最后加'\0'，容易导致一些人误用</font></p> <h4 id="用法1"><a href="#用法1" class="header-anchor">#</a> 用法1</h4> <p>//主要要注意，这种const修饰，表示指向位置的内容不准修改。</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span></span>

<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>



<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token punctuation">{</span>

	string str<span class="token punctuation">;</span>

	str<span class="token operator">=</span><span class="token string">&quot;abscv&quot;</span><span class="token punctuation">;</span>

	<span class="token comment">//主要要注意，这种const修饰，表示指向位置的内容不准修改。</span>

	<span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> p<span class="token operator">=</span>str<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

	

	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%c\n&quot;</span><span class="token punctuation">,</span>p<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%c\n&quot;</span><span class="token punctuation">,</span>p<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	

	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br></div></div><h4 id="用法2（常用）"><a href="#用法2（常用）" class="header-anchor">#</a> 用法2（常用）</h4> <p>用这种复制的方式，就突破了，上一种方式无法修改的桎梏</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span></span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cstring&gt;</span></span>

<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>



<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>

<span class="token punctuation">{</span>

	string str<span class="token punctuation">;</span>

	str<span class="token operator">=</span><span class="token string">&quot;abscv&quot;</span><span class="token punctuation">;</span>

	

	<span class="token keyword">char</span> p<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

	<span class="token comment">//用这种复制的方式，就突破了，上一种方式无法修改的桎梏 </span>

	<span class="token function">strcpy</span><span class="token punctuation">(</span>p<span class="token punctuation">,</span>str<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	

	p<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">'K'</span><span class="token punctuation">;</span> 

	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%c\n&quot;</span><span class="token punctuation">,</span>p<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%c\n&quot;</span><span class="token punctuation">,</span>p<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

	

	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br></div></div><h3 id="_5-2-字符数组→string"><a href="#_5-2-字符数组→string" class="header-anchor">#</a> 5.2.字符数组→string</h3> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token function">string</span> <span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span>	
	string<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">const</span> string<span class="token operator">&amp;</span> str<span class="token punctuation">)</span><span class="token punctuation">;</span>
c<span class="token operator">-</span><span class="token function">string</span> <span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span>	
	string<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">*</span> s<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">character</span> <span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>	
	string<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">char</span> c<span class="token punctuation">)</span><span class="token punctuation">;</span>
initializer <span class="token function">list</span> <span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span>	
	string<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span> <span class="token punctuation">(</span>initializer_list<span class="token operator">&lt;</span><span class="token keyword">char</span><span class="token operator">&gt;</span> il<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">move</span> <span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>	
	string<span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token operator">=</span> <span class="token punctuation">(</span>string<span class="token operator">&amp;&amp;</span> str<span class="token punctuation">)</span> <span class="token keyword">noexcept</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span> </span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cstring&gt;</span> </span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> test<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">~</span><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%s&quot;</span><span class="token punctuation">,</span>test<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		
		string str<span class="token punctuation">;</span>
		str<span class="token operator">=</span>test<span class="token punctuation">;</span><span class="token comment">//字符数组转换为string ，注意，这个是可以的『因为重载了operator::=()』去</span>
        <span class="token comment">//参考：http://www.cplusplus.com/reference/string/string/operator=/</span>
		
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>str<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span> 
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><ul><li>另一种用构造函数写的，但是有坑，那就是只能在构造的时候，初始化时</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;string&gt;</span> </span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;cstring&gt;</span> </span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> test<span class="token punctuation">[</span><span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
	<span class="token keyword">while</span><span class="token punctuation">(</span><span class="token operator">~</span><span class="token function">scanf</span><span class="token punctuation">(</span><span class="token string">&quot;%s&quot;</span><span class="token punctuation">,</span>test<span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		
		string <span class="token function">str</span><span class="token punctuation">(</span>test<span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//注意——只能在初始化的时候，字符数组转换为string </span>
		
		<span class="token comment">//注意——下面这种方式不准！！ </span>
<span class="token comment">//		string str;</span>
<span class="token comment">//		str(test);</span>
		
		<span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span>str<span class="token punctuation">.</span><span class="token function">c_str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span> 
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><h3 id="_5-3-转换string→char"><a href="#_5-3-转换string→char" class="header-anchor">#</a> 5.3.转换<code>string</code>→<code>char</code></h3> <h4 id="运算符重载"><a href="#运算符重载" class="header-anchor">#</a> <code>[]</code>运算符重载</h4> <h4 id="（1）cplusplus-com（我常用这个）上的声明如下"><a href="#（1）cplusplus-com（我常用这个）上的声明如下" class="header-anchor">#</a> （1）cplusplus.com（我常用这个）上的声明如下</h4> <p><font style="background:yellow;">重点：返回的是“字符”，不是什么string类</font></p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">const</span> <span class="token keyword">char</span><span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">(</span>size_t pos<span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>

<span class="token keyword">char</span><span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">(</span>size_t pos<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>有C++教程上写的声明是，显然是参考的这，并且教程讲参考手册的改为这样，增加了可读性。</p> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>
<span class="token keyword">char</span> <span class="token operator">&amp;</span> <span class="token keyword">operator</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">&amp;</span> <span class="token function">at</span><span class="token punctuation">(</span><span class="token keyword">int</span> n<span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="（2）zh-cppreference声明如下"><a href="#（2）zh-cppreference声明如下" class="header-anchor">#</a> （2）zh.cppreference声明如下</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">// 元素访问</span>

    <span class="token keyword">constexpr</span> const_reference <span class="token keyword">operator</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span>size_type pos<span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>
    <span class="token keyword">constexpr</span> reference       <span class="token keyword">operator</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">(</span>size_type pos<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">constexpr</span> const_reference <span class="token function">at</span><span class="token punctuation">(</span>size_type n<span class="token punctuation">)</span> <span class="token keyword">const</span><span class="token punctuation">;</span>
    <span class="token keyword">constexpr</span> reference       <span class="token function">at</span><span class="token punctuation">(</span>size_type n<span class="token punctuation">)</span><span class="token punctuation">;</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><a href="https://www.zhihu.com/question/35614219/answer/798370856" target="_blank" rel="noopener noreferrer">知乎<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a>上对于constexpr的解释</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>constexpr是`C++11`引入的，
1、一方面是为了引入更多的编译时计算能力
2、另一方面也是解决 C++98 的 const 的双重语义问题。

在 C 里面，const 很明确只有「只读」一个语义，不会混淆。
C++ 在此基础上增加了「常量」语义，也由 const 关键字来承担，引出来一些奇怪的问题。
C++11 把「常量」语义拆出来，交给新引入的 constexpr 关键字。

在 C++11 以后，建议凡是「常量」语义的场景都使用 constexpr，只对「只读」语义使用 const。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="_5-4-转换char→string"><a href="#_5-4-转换char→string" class="header-anchor">#</a> 5.4.转换<code>char</code>→<code>string</code></h3> <ul><li>这个转换是最简单的，但是也是一个坑</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">char</span> c<span class="token operator">=</span><span class="token string">'a'</span><span class="token punctuation">;</span>
string str<span class="token punctuation">;</span>	<span class="token comment">//注意，不能使用string str=c;	//因为没有char→string的函数来转换</span>
str<span class="token operator">+=</span>c<span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="_5-5-c语言字符数组和字符到string类"><a href="#_5-5-c语言字符数组和字符到string类" class="header-anchor">#</a> 5.5.C语言字符数组和字符到string类</h3> <p><font size="3" style="background:yellow;">C语言字符数组和字符到string类都很简单，因为都实现了运算符+=和=的重载</font></p> <h4 id="重载（字符用）"><a href="#重载（字符用）" class="header-anchor">#</a> +=重载（字符用）</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> c<span class="token operator">=</span><span class="token string">'9'</span><span class="token punctuation">;</span>
	string str<span class="token operator">=</span><span class="token string">&quot;432&quot;</span><span class="token punctuation">;</span>
	str<span class="token operator">+=</span>c<span class="token punctuation">;</span><span class="token comment">//原型是string&amp; operator+= (char c);</span>
	cout<span class="token operator">&lt;&lt;</span>str<span class="token punctuation">;</span> 
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
 <span class="token punctuation">}</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h4 id="重载（字符用）-2"><a href="#重载（字符用）-2" class="header-anchor">#</a> <code>=</code>重载（字符用）</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> c<span class="token operator">=</span><span class="token string">'9'</span><span class="token punctuation">;</span>
	string str<span class="token operator">=</span><span class="token string">&quot;432&quot;</span><span class="token punctuation">;</span>
	str<span class="token operator">=</span>c<span class="token punctuation">;</span><span class="token comment">//原型是string&amp; operator= (char c);</span>
	cout<span class="token operator">&lt;&lt;</span>str<span class="token punctuation">;</span> 
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//输出9</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h4 id="重载（字符串用）"><a href="#重载（字符串用）" class="header-anchor">#</a> <code>+=</code>重载（字符串用）</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> test<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">&quot;356425462&quot;</span><span class="token punctuation">;</span>
	string str<span class="token punctuation">;</span>
	str<span class="token operator">+=</span>test<span class="token punctuation">;</span><span class="token comment">//原型string &amp; operator+=(const char * s);</span>
	cout<span class="token operator">&lt;&lt;</span>str<span class="token punctuation">;</span> 
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h4 id="重载（字符串用）-2"><a href="#重载（字符串用）-2" class="header-anchor">#</a> <code>=</code>重载（字符串用）</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span><span class="token string">&lt;iostream&gt;</span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
	<span class="token keyword">char</span> test<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token string">&quot;11111&quot;</span><span class="token punctuation">;</span>
	string str<span class="token operator">=</span><span class="token string">&quot;432&quot;</span><span class="token punctuation">;</span>
	str<span class="token operator">=</span>test<span class="token punctuation">;</span><span class="token comment">//原型为 string&amp; operator= (const char* s);</span>
	cout<span class="token operator">&lt;&lt;</span>str<span class="token punctuation">;</span> 
	<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span> 
<span class="token comment">//输出11111</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><h3 id="✔️关于c-的string建议："><a href="#✔️关于c-的string建议：" class="header-anchor">#</a> ✔️关于C++的string建议：</h3> <ul><li>强化string类的成员函数（熟练到，心到码到）</li> <li>深信服的笔试似乎蛮喜欢考字符串的。</li> <li>CCF/CSP好像蛮喜欢那些字符串。</li></ul> <h2 id="✔️int-char-string"><a href="#✔️int-char-string" class="header-anchor">#</a> ✔️int-&gt;char-&gt;string</h2> <ul><li><a href="https://leetcode-cn.com/problems/find-common-characters/" target="_blank" rel="noopener noreferrer">1002. 查找常用字符<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token keyword">class</span> <span class="token class-name">Solution</span> <span class="token punctuation">{</span>
<span class="token keyword">public</span><span class="token operator">:</span>
    vector<span class="token operator">&lt;</span>string<span class="token operator">&gt;</span> <span class="token function">commonChars</span><span class="token punctuation">(</span>vector<span class="token operator">&lt;</span>string<span class="token operator">&gt;</span><span class="token operator">&amp;</span> words<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> resHash<span class="token punctuation">[</span><span class="token number">256</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token function">memset</span><span class="token punctuation">(</span> resHash<span class="token punctuation">,</span> <span class="token number">0x3f</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span> resHash <span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> tempHash<span class="token punctuation">[</span><span class="token number">256</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

        <span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">auto</span> str <span class="token operator">:</span> words <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token function">memset</span><span class="token punctuation">(</span> tempHash<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>tempHash<span class="token punctuation">)</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">auto</span> c <span class="token operator">:</span> str <span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                tempHash<span class="token punctuation">[</span> c <span class="token punctuation">]</span><span class="token operator">++</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

            <span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">256</span><span class="token punctuation">;</span> <span class="token operator">++</span>i <span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                resHash<span class="token punctuation">[</span> i <span class="token punctuation">]</span><span class="token operator">=</span><span class="token function">min</span><span class="token punctuation">(</span> resHash<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">,</span> tempHash<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>

        <span class="token punctuation">}</span>

        vector<span class="token operator">&lt;</span>string<span class="token operator">&gt;</span> res<span class="token punctuation">;</span>
        <span class="token keyword">for</span><span class="token punctuation">(</span> <span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span> i<span class="token operator">&lt;</span><span class="token number">256</span><span class="token punctuation">;</span> <span class="token operator">++</span>i <span class="token punctuation">)</span>
        <span class="token punctuation">{</span>
            <span class="token keyword">while</span><span class="token punctuation">(</span> resHash<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token operator">--</span> <span class="token punctuation">)</span>
            <span class="token punctuation">{</span>
                <span class="token comment">//注意，我的这种方式从int-&gt;char-&gt;string</span>
                <span class="token keyword">char</span> c<span class="token operator">=</span><span class="token punctuation">(</span> i<span class="token operator">-</span><span class="token string">'A'</span> <span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">'A'</span><span class="token punctuation">;</span>
                string temp<span class="token punctuation">;</span>
                temp<span class="token operator">+=</span>c<span class="token punctuation">;</span>
                <span class="token comment">//注意，我的这种方式从int-&gt;char-&gt;string</span>
                res<span class="token punctuation">.</span><span class="token function">push_back</span><span class="token punctuation">(</span> temp <span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> res<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br></div></div><h2 id="『逆向思维』编程技巧"><a href="#『逆向思维』编程技巧" class="header-anchor">#</a> 『逆向思维』编程技巧</h2> <ul><li>反转反转，这种逆向思维，来自高中物理，在代码中也有用到
<ul><li>比如，大数加法</li></ul></li></ul></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div>
    <script src="/assets/js/app.c70e21ad.js" defer></script><script src="/assets/js/57.a9caec0d.js" defer></script>
  </body>
</html>
